package com.F04.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.F04.actionForm.F0405ActionForm;
import com.dao.common.DbOperationExt;
import com.util.CommonTools;

public class F0405Dao extends DbOperationExt {

	Logger log = Logger.getLogger(F0405Dao.class);

	public F0405Dao() {

	}

	public F0405Dao(Connection conn) {
		super(conn);
	}
	
	public List select (F0405ActionForm actionForm) throws SQLException{
		List list = new ArrayList();
		ResultSet rest = null;
		Map map = null;
		StringBuffer sql = new StringBuffer();
		
		String materialName = CommonTools.null2String(actionForm.getMaterialName());
		String supplierName = CommonTools.null2String(actionForm.getSupplierName());
		
		sql.append("select sm.id,m.code,m.name,sm.supplier_code,s.supplier from pc_supplier_materials sm,t_materials m,pc_suppliers s ");
		sql.append(" where sm.material_code = m.code and ");
		sql.append("       s.code = sm.supplier_code");
		
		if(!"".equals(supplierName)){
			sql.append(" and s.supplier like '%"+supplierName+"%' ");
		}
		
		if(!"".equals(materialName)){
			sql.append(" and m.name like '%"+materialName+"%' ");
		}
		
		log.info(sql.toString());
		rest = super.executeQuery(sql.toString());
		while(rest.next()){
			map = new HashMap();
			map.put("id", String.valueOf(rest.getInt("id")));
			map.put("supplierCode", CommonTools.null2String(rest.getString("supplier_code")));
			map.put("supplierName", CommonTools.null2String(rest.getString("supplier")));
			map.put("materialCode", CommonTools.null2String(rest.getString("code")));
			map.put("materialName", CommonTools.null2String(rest.getString("name")));
			list.add(map);
		}
		if(map==null){
			return null;
		}		
		return list;
	}
	
	public void insert(F0405ActionForm actionForm,Map map,int length) throws SQLException{
		
		StringBuffer sql = new StringBuffer();
		List list = new ArrayList();
		for(int i=0;i<length;i++){
			sql = new StringBuffer();
			sql.append("insert into pc_supplier_materials(material_code,supplier_code)");
			sql.append(" values('"+map.get("materialCode"+i)+"','"+CommonTools.null2String(actionForm.getSupplierCode())+"')");
			log.info(sql.toString());
			list.add(sql);
		}		
		super.executeUpdate(list, true);
	}
	
	public void selectByCode(F0405ActionForm actionForm) throws SQLException{
		
		StringBuffer sql = new StringBuffer();
		ResultSet rest = null;
		sql.append("select m.code,m.name,sm.supplier_code,s.supplier from t_materials m,pc_supplier_materials sm,pc_suppliers s");
		sql.append(" where sm.material_code = m.code and ");
		sql.append(" sm.supplier_code = s.code and ");
		sql.append(" sm.id = "+CommonTools.null2String(actionForm.getCode()));
		
		log.info(sql.toString());
		rest = super.executeQuery(sql.toString());
		while(rest.next()){
			actionForm.setMaterialCode(CommonTools.null2String(rest.getString("code")));
			actionForm.setMaterialName(CommonTools.null2String(rest.getString("name")));
			actionForm.setSupplierCode(CommonTools.null2String(rest.getString("supplier_code")));
			actionForm.setSupplierName(CommonTools.null2String(rest.getString("supplier")));
		}
	}
	
	public void update(F0405ActionForm actionForm) throws SQLException{
		
		StringBuffer sql = new StringBuffer();
		sql.append("update pc_supplier_materials set material_code = '"+CommonTools.null2String(actionForm.getMaterialCode())+"'");
		sql.append(",supplier_code = '"+CommonTools.null2String(actionForm.getSupplierCode())+"'");
		sql.append(" where id = "+CommonTools.null2String(actionForm.getCode()));
		
		log.info(sql.toString());
		super.executeUpdate(sql.toString());
	}
	
	public void delete(F0405ActionForm actionForm) throws SQLException{
		
		StringBuffer sql = new StringBuffer();
		sql.append("delete from pc_supplier_materials");
		sql.append(" where id = "+CommonTools.null2String(actionForm.getId()));
		
		log.info(sql.toString());
		super.executeUpdate(sql.toString());
	}
	
	public List initList(String sql) throws SQLException{
		
		ResultSet rest = null;
		Map map = null;
		List list = new ArrayList();
		
		log.info(sql);
		rest = super.executeQuery(sql);
		int i = 0;
		while(rest.next()){
			map = new HashMap();
			map.put("name", CommonTools.null2String(rest.getString("name")));
			map.put("code", CommonTools.null2String(rest.getString("code")));
			list.add(map);
			i++;
		}
		return list;
	}
}
